package br.com.yuri.addressbar;

import com.google.gwt.core.client.JsArray;
import com.google.gwt.maps.client.geocode.Geocoder;
import com.google.gwt.maps.client.geocode.LocationCallback;
import com.google.gwt.maps.client.geocode.Placemark;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.geom.LatLngBounds;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window.Notification;
import com.vaadin.ui.themes.Reindeer;
import com.vaadin.ui.themes.Runo;

/**
 * Server side component for the VMyComponent widget.
 */
@SuppressWarnings("serial")
public class AddressBar extends CustomComponent implements Button.ClickListener{

	private Panel root;
	private TextField addressField;
	private Button searchBtn;
	private LatLng center;
	private LatLngBounds bounds;

	private Geocoder geocoder = new Geocoder();
	
	public AddressBar(){
		CssLayout css = new CssLayout();
        root = new Panel(css);
        root.setStyleName(Reindeer.PANEL_LIGHT);
        css.setStyleName(Runo.CSSLAYOUT_SHADOW);
        css.setWidth("100%");
        HorizontalLayout content = new HorizontalLayout();
        content.setWidth("100%");
        content.setSpacing(true);
        content.setMargin(true);
        addressField = new TextField();
		addressField.setWidth("100%");
		searchBtn = new Button(null);
		searchBtn.setWidth("10em");
		searchBtn.setStyleName(Runo.BUTTON_DEFAULT);
		content.addComponent(addressField);
		content.addComponent(searchBtn);
		content.setExpandRatio(addressField, 1.0f);
		content.setComponentAlignment(searchBtn, Alignment.BOTTOM_CENTER);
		
        css.addComponent(content);
        
        setCompositionRoot(root);
	}

	public String getSearchButtonCaption(){
		return this.searchBtn.getCaption();
	}
	public void setSearchButtonCaption(String caption){
		this.searchBtn.setCaption(caption);
	}
	public String getAddressFieldCaption(){
		return this.addressField.getCaption();
	}
	public void setAddressFieldCaption(String caption){
		this.addressField.setCaption(caption);
	}
	public String getAddressFieldInputPrompt(){
		return this.addressField.getInputPrompt();
	}
	public void setAddressFieldInputPrompt(String input){
		this.addressField.setInputPrompt(input);
	}
	public void setCenter(LatLng center) {
		this.center = center;
	}
	public LatLng getCenter() {
		return center;
	}
	public void setBounds(LatLngBounds bounds) {
		this.bounds = bounds;
	}
	public LatLngBounds getBounds() {
		return bounds;
	}
	
	@Override
	public void buttonClick(ClickEvent event) {
		geocoder.getLocations(addressField.getValue().toString()+"São Paulo, BR", new LocationCallback() {
			@Override
			public void onSuccess(JsArray<Placemark> locations) {
				if (locations.length() == 1){
					setCenter(locations.get(0).getPoint());
				}
			}
			
			@Override
			public void onFailure(int statusCode) {
				AddressBar.this.getWindow().showNotification("Problemas no Endereço!", Notification.TYPE_ERROR_MESSAGE);
			}
		});
	}
	
}
